#include "viml/optimization_passes.h"
#include "viml/ast.h"
#include "viml/instruction.h"
#include "viml/liveness_analyzer.h"
#include "viml/register_allocator.h"
#include <chrono>

namespace viml {

// ========== ConstantFoldingPass 实现 ========== //

bool ConstantFoldingPass::run(Program* program) {
    if (!program || !enabled_) {
        return false;
    }
    
    // 记录开始时间
    auto start = std::chrono::high_resolution_clock::now();
    
    // 重置folder状态
    folder_.reset();
    
    // 执行常量折叠
    program->accept(folder_);
    
    // 获取结果
    auto result = folder_.get_result();
    bool changed = folder_.has_changed();
    
    if (changed && result) {
        // 将优化后的结果复制回原program
        // 由于Program的成员是public的,我们可以直接替换statements
        auto* optimized_program = dynamic_cast<Program*>(result.get());
        if (optimized_program) {
            // 移动statements
            program->statements = std::move(optimized_program->statements);
        }
    }
    
    // 记录结束时间
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double, std::milli> duration = end - start;
    
    // 更新统计
    statistics_.run_count++;
    statistics_.transformations += folder_.get_fold_count();
    statistics_.time_spent_ms += duration.count();
    statistics_.changed = changed;
    
    return changed;
}

// ========== DeadCodeEliminationPass 实现 ========== //

bool DeadCodeEliminationPass::run(Program* program) {
    if (!program || !enabled_) {
        return false;
    }
    
    // 记录开始时间
    auto start = std::chrono::high_resolution_clock::now();
    
    // 创建shared_ptr包装(使用空删除器)
    auto program_ptr = std::shared_ptr<Program>(program, [](Program*){});
    
    // 执行死代码消除
    auto result = eliminator_.eliminate_dead_code(program_ptr);
    
    bool changed = eliminator_.has_changed();
    
    // 记录结束时间
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double, std::milli> duration = end - start;
    
    // 更新统计
    statistics_.run_count++;
    statistics_.transformations += eliminator_.get_eliminated_count();
    statistics_.time_spent_ms += duration.count();
    statistics_.changed = changed;
    
    return changed;
}

// ========== RegisterAllocationPass 实现 ========== //

bool RegisterAllocationPass::run(Bytecode* bytecode) {
    if (!bytecode || !enabled_) {
        return false;
    }
    
    // 记录开始时间
    auto start = std::chrono::high_resolution_clock::now();
    
    // 创建shared_ptr包装(使用空删除器)
    auto bytecode_ptr = std::shared_ptr<Bytecode>(bytecode, [](Bytecode*){});
    
    // 执行活性分析
    LivenessAnalyzer analyzer;
    if (!analyzer.analyze(bytecode_ptr)) {
        return false;
    }
    
    // 执行寄存器分配
    RegisterAllocator allocator;
    bool changed = allocator.allocate(bytecode_ptr, analyzer);
    
    // 记录结束时间
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double, std::milli> duration = end - start;
    
    // 更新统计
    statistics_.run_count++;
    statistics_.transformations += allocator.get_reuse_count();
    statistics_.time_spent_ms += duration.count();
    statistics_.changed = changed;
    
    return changed;
}

} // namespace viml
